;(function($){
	$.Dialog=$.Widget.extend({
		init: function(params){
			params=params||{};
			var title=params.title||'Dialog', content=params.content||'No Content', ok=params.ok, cancel=params.cancel, button=params.button||{};
			this.buttons={};
			if(ok){button.ok=ok;}
			if(cancel){button.cancel=cancel;}
			var buttonHtml='';
			for(var b in button){
				if(!button[b].title){continue;}
				var bId=$.getIdSeed();
				buttonHtml=buttonHtml.concat('&nbsp;&nbsp;<button id="',bId,'" type="button" tabindex="0" class="gwt-Button">',button[b].title,'</button>');
				this.buttons[bId]=$.isFunction(button[b].click)?button[b].click:$.noop;
			}
			if(!buttonHtml){
				buttonHtml='<button type="button" tabindex="0" class="gwt-Button button-close">Close</button>';
			}
			if($.isIE6){
				this.iframe=$('<IFRAME style="display:none;BORDER-RIGHT:0px;PADDING-RIGHT:0px;BORDER-TOP:0px;PADDING-LEFT:0px;FILTER:alpha(opacity=100);LEFT:292px;VISIBILITY:visible;PADDING-BOTTOM:0px;MARGIN:0px;BORDER-LEFT:0px;PADDING-TOP:0px;BORDER-BOTTOM:0px;POSITION:absolute;TOP:0px;" src="javascript:\'\'" frameBorder=0 scrolling=no></IFRAME>');
			}
			this.el=$(''.concat('<div style="display:none;overflow:visible;left:300px;top:101px;visibility:visible;position:absolute;clip:rect(auto,auto,auto,auto);" class="gwt-DialogBox"><div><table cellspacing="0" cellpadding="0"><tbody><tr class="dialogTop"><td class="dialogTopLeft"><div class="dialogTopLeftInner"></div></td><td class="dialogTopCenter"><div class="dialogTopCenterInner"><div class="Caption">',
					title,'</div></div></td><td class="dialogTopRight"><div class="dialogTopRightInner"></div></td></tr><tr class="dialogMiddle"><td class="dialogMiddleLeft"><div class="dialogMiddleLeftInner"></div></td><td class="dialogMiddleCenter"><div class="dialogMiddleCenterInner dialogContent"><table cellspacing="4" cellpadding="0"><tbody><tr><td align="center" style="vertical-align: top;"><div class="gwt-HTML Content">',
					content,'</div></td></tr><tr><td align="center" style="vertical-align: top;"></td></tr><tr><td align="right" style="vertical-align: top;">',
					buttonHtml,'</td></tr></tbody></table></div></td><td class="dialogMiddleRight"><div class="dialogMiddleRightInner"></div></td></tr><tr class="dialogBottom"><td class="dialogBottomLeft"><div class="dialogBottomLeftInner"></div></td><td class="dialogBottomCenter"><div class="dialogBottomCenterInner"></div></td><td class="dialogBottomRight"><div class="dialogBottomRightInner"></div></td></tr></tbody></table></div></div>'));
			this.el.bind('click',this,function(event){
				var el=$(event.srcElement||event.target);
				var dialog=event.data;
				if(el.is('.gwt-Button.button-close')){dialog.destroy();}
				var click=dialog.buttons[$.id(el)];
				if($.isFunction(click)){click.call(dialog,event);}
			});
		},getEl:function(){
			return this.el;
		},close:function(){
			this.destroy();
		},destroy: function(){
			if(this.iframe){this.iframe.remove();}
			this.el.unbind('click');
			this.el.remove();
		},show: function(){
			var css={display:''};
			if(this.iframe){this.iframe.appendTo(document.body);}
			this.el.appendTo(document.body);
			var elW=this.el.width(), elH=this.el.height();
			if(elW<100){elW=100;}if(elH<100){elH=68;}
			css.left=($(document).width()-elW)/2+'px';
			css.top=($(document).height()-elH)/3+'px';
			if(this.iframe){
				this.iframe.css(css).width(elW).height(elH);
			}
			this.el.css(css);
			return this.el;
		}
	});
	$.AutoComplete=$.Widget.extend({
		init: function(){
			this.KEY={UP:38,DOWN:40,ENTER:13,ESC:27};
			this.el=$('<div class="TC_AutoComplete" style="display:none;"></div>');
			this.current=null;this.currentData={};this.currentKey='';
			this.jsonOrUrlOrFunction=null;this.callback=$.noop;
			this.bind();
		},bind:function(){
			this.el.bind('mousedown',this,function(event){
				var el=$(event.srcElement||event.target);
				var ac=event.data;
				var tmp = el.is('td')?el:el.is('tr')?el.children(':first'):null;
				if(tmp&&tmp.size()==1&&$.isFunction(ac.callback)){
					ac.setValue(tmp);
				}
			});
			this.el.bind('mouseover',function(event){
				var el=$(event.srcElement||event.target);
				if(el.is('td')){el.parent().addClass('mouseover');}
				else if(el.is('tr')){el.addClass('mouseover');}
			});
			this.el.bind('mouseout',function(event){
				var el=$(event.srcElement||event.target);
				if(el.is('td')){el.parent().removeClass('mouseover');}
				else if(el.is('tr')){el.removeClass('mouseover');}
			});
		},getEl:function(){
			return this.el;
		},setValue:function($td){
			this.callback(this.currentData[$td.attr('acid')],this.current);
		},getData:function(){
			var json=[], juf=this.jsonOrUrlOrFunction;
			if($.isFunction(juf)){json=juf(this.current)||json;}
			if($.isArray(juf)){json=juf||json;}
			if($.isString(juf)){json=$.json(juf,false)||json;}
			return json;
		},suggest:function(event){
			$eo=$(event.srcElement||event.target);
			var key=event.keyCode, elVal=$.trim($eo.val());
			if(!elVal){this.el.hide();return this;}
			if(key&&(key==this.KEY.UP||key==this.KEY.DOWN||key==this.KEY.ENTER||key==this.KEY.ESC)){
				var selected=this.el.find('tr.mouseover:first');
				if(key==this.KEY.UP){
					if(selected.size()<1){return this;}
					var prev=selected.removeClass('mouseover').prev();
					if(prev.size()){prev.addClass('mouseover');}
				}else if(key==this.KEY.DOWN){
					if(selected.size()<1){this.el.find('tr:first').addClass('mouseover');}
					var next=selected.removeClass('mouseover').next();
					if(next.size()){next.addClass('mouseover');}
				}else if(key==this.KEY.ENTER){
					if(selected.size()){this.setValue(selected.children(':first'));}
				}else if(key==this.KEY.ESC){
					this.hide();
				}
				return this;
			}
			var currentId=$.id(this.current), eoId=$.id($eo), data;
			var type=event.type, isAfter=$eo.next('.TC_AutoComplete').size()>0;
			if(currentId==eoId){
				if(this.currentKey&&elVal.indexOf(this.currentKey)>-1&&!$.isEmptyObject(this.currentData)){}
				else{data=this.getData()||[];this.currentKey=elVal;}
			}else{
				if(this.current&&this.currentData){
					for(var p in this.currentData){delete this.currentData[p];}
				}
				this.current=$eo;
				this.jsonOrUrlOrFunction=(event.data?event.data.jsonOrUrlOrFunction:null)||null;
				this.callback=(event.data?event.data.callback:$.noop)||$.noop;
				this.currentKey=elVal;
				data=this.getData()||[];
			}
			if(!isAfter){$eo.after(this.el);}
			var pos=$eo.position(), height=$eo.height(), width=$eo.width(); width=width<100?100:width;
			var css={width:width,top:(pos.top+height)+'px',left:(pos.left+1)+'px',display:'block'};
			this.el.css(css);
			var html='', count=0;
			if(data){
				for(var i=0;i<50&&i<data.length&&elVal.length>0;i++){
					var text=data[i].text, id=data[i].id;
					if(id&&text&&text.indexOf(elVal)>-1){
						this.currentData[id]=data[i];
						html=html.concat('<tr><td align="left" height="20px" acid="',id,'">',text,'</td></tr>');
						count++;
					}
				}
			}else{
				for(var p in this.currentData){
					var text=this.currentData[p].text, id=p;
					if(id&&text&&text.indexOf(elVal)>-1){
						html=html.concat('<tr><td align="left" height="20px" acid="',id,'">',text,'</td></tr>');
						count++;
					}
				}
			}
			if(count>10){
				css.height='220px';
			}
			html=''.concat('<table align="left" cellpadding="0" cellspacing="0" width="100%">',html,'</table>');
			this.el.empty().append(html);
			return this;
		},hide:function(){
			this.el.hide(100);
			return this;
		}
	});
	$.fn.extend({suggest: function(jsonOrUrlOrFunction,callback){
		$.AutoComplete.__autoComplete=$.AutoComplete.__autoComplete?$.AutoComplete.__autoComplete:new $.AutoComplete();
		return this.each(function(){
			var $this=$(this),data={jsonOrUrlOrFunction:jsonOrUrlOrFunction,callback:callback};
			$this.bind('click',data,function(event){
				$.AutoComplete.__autoComplete.suggest(event);
			});
			$this.bind('keyup',data,function(event){
				$.AutoComplete.__autoComplete.suggest(event);
			});
			$this.bind('blur',function(event){
				$.AutoComplete.__autoComplete.hide();
			});
		});
	}});
	$(document).ready(function(){
		$('#autoComplete1, #autoComplete2').suggest('autocomplete.json',function(data,$el){
			$el.val(data.text);
		});
	});
	$.Cache=$.Widget.extend({
	  init: function(){
		this.cache={};
	},select:function(cacheTarget,cacheObject){
		if(cacheTarget.current){
			if(!cacheTarget.current.cachable){
				cacheTarget.current.el.remove();
			}else{
				cacheTarget.current.el.css({display:'none'});
			}
		}
		cacheObject.el.css({display:''});
		cacheTarget.current=cacheObject;
	},cacheLoad: function($src,$target,url,params,callback){
		if(!($src&&$target&&typeof(url)==="string")){return this;}
		var targetId=$.id($target);
		var srcId=$.id($src);
		var cacheTarget=this.cache[targetId];
		if(!cacheTarget){
			cacheTarget={clone:$target.empty().clone().css({display:'none'}).attr('id',$.getIdSeed()),current:{el:$target,cachable:true},cache:{}};
			this.cache[targetId]=cacheTarget;
		}
		var cacheObject=cacheTarget.cache[srcId];
		if(cacheObject){
			this.select(cacheTarget,cacheObject);
			return this;
		}
		var off=url.indexOf(' ');
		if(off>-1){
			var selector=url.slice(off,url.length);
			url=url.slice(0,off);
		}
		var type='GET';
		if(params){
			if($.isFunction(params)){
				callback=params;
				params=null;
			} else if(typeof(params)==='object'){
				params=$.param(params,$.ajaxSettings.traditional);
				type='POST';
			}
		}
		var self=this;
		$.ajax({url:url,type:type,dataType:'html',data:params,
			complete: function(res,status){
				if(status==='success'||status==='notmodified'){
					var $obj=cacheTarget.clone.clone().attr('id',$.getIdSeed());
					$target.after($obj);
					$obj.append(selector?$('<div />').append(res.responseText.replace(/<script(.|\s)*?\/script>/gi, '')).find(selector):res.responseText);
					var children=$obj.children('.TCExecutable');
					var jsscript='';
					children.each(function(){jsscript=jsscript.concat('{',$(this).attr('script'),'}');});
					var cachable=!(children.attr('cachable')==='false');
					var cObject={el:$obj,script:jsscript,cachable:cachable};
					if(jsscript){eval(jsscript);}
					if(cachable){cacheTarget.cache[srcId]=cObject;}
					self.select(cacheTarget, cObject);
				}
				if(callback){
					$target.each(callback, [res.responseText, status, res]);
				}
			}
		});
		return this;
	}
	});
	$.fn.extend({
		cacheLoad: function($target,url,params,callback){
			$.Cache.__cache=$.Cache.__cache?$.Cache.__cache:new $.Cache();
			$.Cache.__cache.cacheLoad(this,$target,url,params,callback);
			return this;
		}
	});
})(jQuery);